package cn.yudao.ymcc.service.impl;

import cn.yudao.ymcc.domain.MessageBlacklist;
import cn.yudao.ymcc.domain.MessageSms;
import cn.yudao.ymcc.mapper.MessageBlacklistMapper;
import cn.yudao.ymcc.mapper.MessageSmsMapper;
import cn.yudao.ymcc.service.IMessageSmsService;
import cn.yudao.ymcc.utils.AssertUtil;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;


/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author whale.jia
 * @since 2022-08-07
 */
@Service
public class MessageSmsServiceImpl extends ServiceImpl<MessageSmsMapper, MessageSms> implements IMessageSmsService {

    @Autowired
    private MessageBlacklistMapper blacklistMapper;
    /**
    * @Title: shield
    * @Description: 拉黑业务层
    * @Author: Mr.Jia
    * @Version: 1.0
    * @Date:  2022/8/8 9:26
    * @Parameters:
    * @Return
    */
    @Override
    public void shield(MessageSms messageSms) {
        // 查询黑名单表中是否有messageSms 根据电话号作为唯一标识查询
        AssertUtil.isTrue( extracted(messageSms),"该用户已被拉黑，请勿重复拉黑");
        // 新增黑名单数据库
        inserterMessageBlacklist(messageSms);
    }

    /**
    * @Title: inserterMessageBlacklist
    * @Description: 添加数据到黑名单数据库
    * @Author: Mr.Jia
    * @Version: 1.0
    * @Date:  2022/8/8 10:00
    * @Parameters:
    * @Return
    */
    public void inserterMessageBlacklist(MessageSms messageSms) {
        // 将messagesms对象转换为MessageBlacklist对象
        MessageBlacklist messageBlacklist = new MessageBlacklist();
        messageBlacklist.setIp(messageSms.getIp());
        messageBlacklist.setPhone(messageSms.getPhone());
        messageBlacklist.setUserId(messageSms.getUserId().intValue());
        messageBlacklist.setText(messageSms.getContent());
        blacklistMapper.insert(messageBlacklist);
    }

    /**
    * @Title: extracted
    * @Description: 判读黑名单中是否有此用户
    * @Author: Mr.Jia
    * @Version: 1.0
    * @Date:  2022/8/8 9:36
    * @Parameters:
    * @Return
    */
    public boolean extracted(MessageSms messageSms) {
        // 查询黑名单表中是否有messageSms 根据电话号作为唯一标识查询
        Wrapper entityWrapper = new EntityWrapper();
        entityWrapper.eq("phone", messageSms.getPhone());
        List list = blacklistMapper.selectList(entityWrapper);
        if (list == null || list.size() > 0){
            return false;
        }
        return true;
    }

}
